/**
 * 
 */
package com.ekingstar.framework.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import com.opensymphony.xwork2.ActionSupport;
import com.ekingstar.framework.domain.Function;
import com.ekingstar.framework.domain.FunctionField;
import com.ekingstar.framework.domain.Role;
import com.ekingstar.framework.service.IFunctionService;
import com.ekingstar.framework.service.IRoleService;

/**
 * 
 *
 */
@Action(name="grant",namespace="/role")
@Result(name="list",value="list",type=ServletActionRedirectResult.class,params={"namespace","/role"})
public class GrantFunctionAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IRoleService roleService;
	private IFunctionService functionService;
	
	private Role role;
	private String nodeType;
	
	public void setMyNodeType(String nodeType) {
		this.nodeType = nodeType;
	}
	public void setFunctionService(IFunctionService functionService) {
		this.functionService = functionService;
	}
	private String nodeId;
	public String getNodeId() {
		return nodeId;
	}
	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	public String getChilden(){
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter out = response.getWriter();
			response.setContentType("application/json; charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			
			//功能
			if (nodeType != null && nodeType.trim().equals("fun")){
				if (nodeId == null || nodeId.trim().equals("") || nodeId.trim().equals("-1") ){
					nodeId = null;
				}
				List children = functionService.getChildren(nodeId);
				if (children != null && children.size() > 0)
				{
					List rolesFunction = roleService.getFunctionsByRoleId(role.getId());
					JSONArray json = new JSONArray();
					for (int i = 0 ;i < children.size();i++){
						Function fun = (Function)children.get(i);
						JSONObject e = new JSONObject();
						e.element("id", fun.getId());
						e.element("name", fun.getName());
						e.element("leaf", false);
						if (fun.getLeaf() == null || !fun.getLeaf().booleanValue() ){
							e.element("iconCls", "folder");
							e.element("myNodeType","fun");
						}
						else {
							e.element("iconCls", "Benz");
							if (rolesFunction != null && rolesFunction.contains(fun)){
								e.element("checked", true);
							}
							else {
								e.element("checked", false);
							}
							e.element("myNodeType","field");
						}
						json.add(e);
					}
					//
					out.print(json.toString());
				}
			}
			//传入的是功能叶子节点
			else if (nodeType != null && nodeType.trim().equals("field")){
				List fields = functionService.getFunctionField(nodeId);
				if (fields != null && fields.size()>0){
					JSONArray json = new JSONArray();
					List roleFields = roleService.getFieldsByRoleId(role.getId(),nodeId);
					for (int i = 0 ;i < fields.size();i++){
						FunctionField field = (FunctionField)fields.get(i);
						JSONObject e = new JSONObject();
						e.element("iconCls", "Benz");
						e.element("id", field.getId());
						if(FunctionField.INPUT.equals(field.getFieldClass())){
							e.element("name", "输入字段");
						}else if(FunctionField.OUTPUT.equals(field.getFieldClass())){
							e.element("name", "输出字段");
						}else if(FunctionField.ACTION.equals(field.getFieldClass())){
							e.element("name", "操作");
						}
						e.element("fieldDesc", field.getFieldDesc());
						e.element("fieldClass", field.getFieldClass());
						e.element("leaf", true);
						if (roleFields != null && roleFields.contains(field)){
							e.element("checked", true);
						}else{
							e.element("checked", false);
						}
						
						json.add(e);
					}
					//
					out.print(json.toString());
				}
			}
			
			
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}
	public String input(){
		if (role != null && role.getId() != null){
			role = roleService.viewRole(role.getId());
		}
		return "input";
	}
	public String execute(){
		roleService.grantFunctionToRole(role);
		return "list";
	}
}
